import Reflux from 'reflux';
import Actions from './Actions';
import update from 'react-addons-update';
import Utils from "../../../../utils/index";

import { Control } from 'react-keeper'
import { Toast, Modal } from 'antd-mobile';

import DB from '../../../../app/db';
import StorageUtils from "../../../../app/storage";

export default Reflux.createStore({
  listenables: [Actions],
  data: {
    doorId: '',
    navBarText: '',
    IC: '',
    name: '',
    phone: '',
    gender: 1,
    proprietorId: '',
    stepStatus: 0,
    icExist: false,
    NoticeBar: {
      visible: false,
      text: ''
    },
    door: {}
  },

  customInit(doorId) {
    this.data.doorId = doorId;
    this.data.IC = '';
    this.data.navBarText = '身份证验证';
    this.data.proprietorId = '';
    this.data.name = '';
    this.data.phone = '';
    this.data.gender = 1;
    this.data.stepStatus = 0;
    this.data.icExist = false;
    this.updateComponent();
    this.initDoorInfo();
  },

  initDoorInfo() {
    let t = this;
    DB.DoorAuth.findDoorInfoById({id: this.data.doorId}).then(
      function (content) {
        if (content) {
          t.data.door = content;
          t.updateComponent();
        }
      },
      function (error) {
        console.log(error);
      }
    );
  },

  onNavBarClick(key) {
    let t = this;
    let stepStatus = t.data.stepStatus;
    switch (key) {
      case 'next':
        switch (stepStatus) {
          case 0:
            if (Utils.isCardID(t.data.IC)) {
              const proprietor = StorageUtils.getStorageItem('proprietor');
              const {identityCard = null} = proprietor;
              if (t.data.IC === identityCard) {
                Toast.fail('不能授权给自己！');
              } else {
                DB.DoorAuth.getProprietorByIC({card: t.data.IC}).then(
                  function (content) {
                    if (content && content.id !== null) {
                      t.data.proprietorId = content.id;
                      t.data.phone = content.phone;
                      t.data.name = content.name;
                      t.data.gender = content.gender;
                      t.data.icExist = true;
                      t.updateComponent();
                    } else {
                      t.data.proprietorId = '';
                      t.data.phone = '';
                      t.data.name = '';
                      t.data.gender = 1;
                      t.data.icExist = false;
                      t.updateComponent();
                    }
                  },
                  function (error) {
                    console.log(error);
                  }
                );
                t.data.navBarText = '信息完善';
                t.data.stepStatus = stepStatus + 1;
                t.updateComponent();
              }
            } else {
              Toast.fail('身份证号码错误！');
            }
            break;
          case 1:
            let checkPhone = Utils.checkPhone(t.data.phone);
            if (checkPhone.success) {
              t.data.stepStatus = stepStatus + 1;
              t.data.navBarText = '授权信息确认';
              t.data.NoticeBar.visible = false;
              t.data.NoticeBar.text = '';
              t.updateComponent();
            } else {
              t.data.NoticeBar.visible = true;
              t.data.NoticeBar.text = '您输入的手机号码有误！';
              t.updateComponent();
            }
            break;
          case 2:
            break;
          case 3:
            break;
          default:
            break;
        }
        break;
      case 'pre':
        t.data.stepStatus = stepStatus - 1;
        t.data.NoticeBar.visible = false;
        t.data.NoticeBar.text = '';
        t.updateComponent();
        break;
      case 'auth':
        DB.DoorAuth.auth({
          authorizer: StorageUtils.getStorageItem('proprietor').id,
          doorplateId: t.data.door.doorId,
          proprietorId: t.data.proprietorId,
          name: t.data.name,
          idNo: t.data.IC,
          phone: t.data.phone,
          gender: t.data.gender,
          // 授权
          auth: true
        }).then(
          function (content) {
            console.log(content);
          },
          function (error) {
            console.log(error);
          }
        );
        t.data.stepStatus = stepStatus + 1;
        t.data.navBarText = '授权结果';
        t.updateComponent();
        break;
      case 'close':
        Control.replace('/home/my');
        break;
      default:
        break;
    }
    this.updateComponent();
  },

  onICChange(val) {
    this.data.IC = val;
    this.updateComponent();
  },

  onPhoneChange(val) {
    this.data.phone = val;
    this.updateComponent();
  },

  onNameChange(val) {
    this.data.name = val;
    this.updateComponent();
  },

  onGenderChange(val) {
    this.data.gender = val[0];
    this.updateComponent();
  },

  onNoticeBarClick() {
    this.data.NoticeBar.visible = false;
    this.data.NoticeBar.text = '';
    this.updateComponent();
  },

  updateComponent: function () {
    this.trigger(this.data);
  },

  getInitialState: function () {
    return this.data;
  }

});
